package com.study.samsung.Control;

import com.alibaba.fastjson.JSONObject;
import com.study.samsung.Entity.*;
import com.study.samsung.Service.GoodsService;
import com.study.samsung.Service.IndexSearchService;
import com.study.samsung.Service.ProductDetailService;
import com.study.samsung.Utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@WebServlet("/productDetails")
public class ProductDetailsServlet extends BaseServlet {
    ProductDetailService pds = new ProductDetailService();
    IndexSearchService iss = new IndexSearchService();
    GoodsService gds = new GoodsService();

    @Override
    public String excute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("lid");
        String sid = req.getParameter("sid");
        Integer clid = (Integer) session.getAttribute("lid");
        Integer colsid = (Integer) session.getAttribute("colsid");
        session.setAttribute("ssid",sid==null?colsid:Integer.parseInt(sid));
        session.setAttribute("slid",id==null?clid:Integer.parseInt(id));

        List<Commodity> newgoods = iss.selectnew();
        List<Succession> selectxl = iss.selectxl();

        req.setAttribute("newgoods",newgoods);
        req.setAttribute("selectxl",selectxl);

        Object user = session.getAttribute("user");
        if (user!=null){
            User nowuser = (User) user;
            Integer uid = nowuser.getUid();

            session.setAttribute("cartcount",gds.cartcount(uid));
        }
        return ("f:productDetails");
    }
    public String selectAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer lid = (Integer) session.getAttribute("slid");
        Integer sid = (Integer) session.getAttribute("ssid");
        List<Commodity> list =  pds.selectAll(lid);
        String json = JSONObject.toJSONString(list);
        System.out.println("7888");
        System.out.println(json);
        return json;
    }
    public JSONObject change(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
//        System.out.println(id);
        Integer sid =Integer.parseInt(id);
        session.setAttribute("csid",sid);
        Integer nnid = (Integer) session.getAttribute("nid");
        Integer nid=nnid==null?1:nnid;
        System.out.println("前端传的nid："+nid);
        Commodity com = pds.selectOne(sid,nid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("com",com);
        jsonObject.put("nid",nid);
        return jsonObject;
    }


    public String memory(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        Integer sid = (Integer) session.getAttribute("ssid");
//        System.out.println("789");
//        System.out.println(sid);
        List<Commoditymemory> list = pds.selectmemory(sid);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
//        System.out.println("");
        return json;
    }

    public JSONObject selectAllmemory(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        Integer nid =(Integer) session.getAttribute("nid");
        List<Commoditymemory> list = pds.selectmemory(Integer.parseInt(id));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("nid",nid);
        String json = JSONObject.toJSONString(list);
//        System.out.println(json);
        return jsonObject;
    }

    public String memorychange(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        String id=request.getParameter("id");
        System.out.println(id);
        String nid=request.getParameter("nid");
        Integer sid = Integer.parseInt(id);
        session.setAttribute("nid",Integer.parseInt(nid));
        Commoditymemory commy=pds.selectmemoryone(sid,Integer.parseInt(nid));
        String json = JSONObject.toJSONString(commy);
        System.out.println(json);
        System.out.println("......");
        return json;
    }
    public String prohibit(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer sid = (Integer) session.getAttribute("ssid");
        Integer csid = (Integer) session.getAttribute("csid");
        Integer nid = (Integer) session.getAttribute("nid");
        List<Detail> list = pds.prohibit(nid==null?1:nid,csid==null?sid:csid);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    public String prohibitColor(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer sid = (Integer) session.getAttribute("ssid");
        Integer csid = (Integer) session.getAttribute("csid");
        Integer yid = (Integer) session.getAttribute("yid");
        List<Detail> list = pds.prohibitColor(yid==null?1:yid,csid==null?sid:csid);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    public String color(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");

        List<Commoditycolor> list = pds.selectcolor();
        String json = JSONObject.toJSONString(list);
//        System.out.println(json);
        return json;
    }
    public String colorYid(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String id = request.getParameter("yid");
        Integer yid = Integer.parseInt(id);
        session.setAttribute("yid",yid);
        return null;
    }
    public String selectimg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer sid = (Integer) session.getAttribute("ssid");
//        System.out.println("753");
//        System.out.println(sid);
        List<Commodityimg> list = pds.selectimg(sid);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }
    public String selectAllimg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        String id = request.getParameter("id");
        List<Commodityimg> list = pds.selectimg(Integer.parseInt(id));
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    public String selectAllxiaimg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");

        String id = request.getParameter("id");
        List<Commodityimg> list = pds.selectxiaimg(Integer.parseInt(id));
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    public String selectxiaimg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        Integer sid = (Integer) session.getAttribute("ssid");
        List<Commodityimg> list = pds.selectxiaimg(sid);
        String json = JSONObject.toJSONString(list);
        System.out.println(json);
        return json;
    }

    //收藏
    public String insertLike(HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User) session.getAttribute("user");
        Integer lid = (Integer) session.getAttribute("slid");
//        Integer sid = (Integer) session.getAttribute("ssid");
        Integer csid = (Integer) session.getAttribute("csid");
        System.out.println(csid);
        Date d = new Date();
        long time = d.getTime();
        java.sql.Date date = new java.sql.Date(time);
        Commodity comm=pds.selectSid(lid);
        Integer sid = comm.getSid();
        if (user != null) {
            Integer num = pds.selectByOne(csid==null?sid:csid, user.getUid());
            System.out.println(num);
            if (num == 0) {
                Integer integer = pds.insertLike(user.getUid(), csid==null?sid:csid, String.valueOf(date));
                return "收藏成功";
            } else {
                Integer integer = pds.deleteOne(csid==null?sid:csid);
                return "取消收藏";
            }
        } else {
            return "null";
        }
    }
    //从商品列表页进之后如果登录显示是否收藏，如果没登录显示没收藏
    public String isLike(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("utf-8");
        //设置响应内容的主体格式，是json格式的
        response.setContentType("application/json;charset=utf-8");
        User user = (User) session.getAttribute("user");
                Integer lid = (Integer) session.getAttribute("slid");
//        Integer sid = (Integer) session.getAttribute("ssid");
        Integer csid = (Integer) session.getAttribute("csid");
        Commodity comm=pds.selectSid(lid);
        Integer sid = comm.getSid();
        Integer num = pds.selectByOne(csid==null?sid:csid, user.getUid());
        if (user!=null){
            if (num==0){
                return "0";
            }else{
                return "1";
            }
        }else{
            return "null";
        }

    }

//    加入购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Integer lid = (Integer) session.getAttribute("slid");
        Integer csid = (Integer) session.getAttribute("csid");
        Integer nid = (Integer) session.getAttribute("nid");
        Integer yid = (Integer) session.getAttribute("yid");
//        session.removeAttribute("ssid");
        session.removeAttribute("csid");
        session.removeAttribute("nid");
        session.removeAttribute("yid");
        String num = request.getParameter("num");
        String price = request.getParameter("price");
        Integer nums= Integer.parseInt(num);
        BigDecimal sprice=BigDecimal.valueOf(Integer.parseInt(price));

        Commodity comm=pds.selectSid(lid);
        System.out.println("列表也进入默认第一个商品："+comm);
        Integer sid = comm.getSid();
        User user = (User) session.getAttribute("user");
        Detail detail = pds.selectOneDetail(nid==null?1:nid,yid==null?1:yid,csid==null?sid:csid);
        ShoppingCart scart = pds.addCartOne(detail.getSxid());

        if (user!=null){
            if (scart==null){
                Integer integer = pds.addCart(nums,detail.getSxid(), user.getUid(), sprice);
                if (integer>0){
                    return "加入购物车";
                }else{
                    return "加入失败";
                }
            }else{
                Integer tnum = nums+ scart.getGnum();
                Integer integer= pds.updateCart(detail.getSxid(),tnum,sprice);
                return "加入购物车";
            }
        }else{
            return "null";
        }
    }

//    点击购买
    public String buy(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Integer lid = (Integer) session.getAttribute("slid");
//        session.removeAttribute("ssid");
        Integer csid = (Integer) session.getAttribute("csid");
        session.removeAttribute("csid");
        Integer nid = (Integer) session.getAttribute("nid");
        session.removeAttribute("nid");
        Integer yid = (Integer) session.getAttribute("yid");
        session.removeAttribute("yid");
        String num = request.getParameter("num");
        Integer nums= Integer.parseInt(num);

        Commodity comm=pds.selectSid(lid);
        Integer sid = comm.getSid();
        User user = (User) session.getAttribute("user");
        Detail detail = pds.selectOneDetail(nid==null?1:nid,yid==null?1:yid,csid==null?sid:csid);
        System.out.println(detail);
        System.out.println("4444444");
        System.out.println(csid==null?sid:csid);
        if (user!=null){
            session.setAttribute("sxid",detail.getSxid());
            session.setAttribute("nums",nums);
            return "立即购买";
        }else{
            return "null";
        }
    }
}
